package com.zzptc.lxy.mybook;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.SharedPreferencesCompat;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zzptc.lxy.mybook.Bean.Book;
import com.zzptc.lxy.mybook.Bean.User;

import org.litepal.LitePal;

public class LoginActivity extends AppCompatActivity {
    private EditText usnedit, psdedit;
    private Button loginbtn, registerbtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        getBook();
        getUser();
        initView();
        setListener();
    }

    private void getBook() {
        new Book(0, "小妇人", "路易莎·梅·奥尔科特", R.mipmap.littlewomen).save();
        new Book(0, "羊脂球", "莫泊桑", R.mipmap.yangzhiqiu).save();
        new Book(0, "佛耶戈", "英雄联盟", R.mipmap.foyeg).save();
        new Book(0, "万族之劫", "老鹰吃小鸡", R.mipmap.wzzj).save();
        new Book(0, "三界劳改局", "一梦黄粱", R.mipmap.sjlgj).save();
        new Book(0, "兔子必须死", "一梦黄粱", R.mipmap.tzbxs).save();
        new Book(0, "沙丘", "弗兰克・赫伯特", R.mipmap.dune).save();
        new Book(0, "起风了", "堀辰雄", R.mipmap.windy).save();
        new Book(0, "追风筝的孩子", "卡勒德·胡赛尼", R.mipmap.thekiterunner).save();

    }

    private void getUser() {
        new User(0, "张三", "123").save();
        new User(0, "李四", "456").save();
        new User(0, "王五", "789").save();

    }

    private void setListener() {
        loginbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String usn = usnedit.getText().toString().trim();
                String psd = psdedit.getText().toString().trim();
                User u = confirmUser(usn, psd);
                if (u == null) {
                    //登录失败
                } else {
                    showToast("登录成功," + usn + ",欢迎回来！");
                    Intent it = new Intent(LoginActivity.this, MainActivity.class);
                    SharedPreferences sp = getSharedPreferences("userinfo", MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putInt("userid", u.getId());
                    editor.putString("username", u.getUsername());
                    editor.apply();
                    startActivity(it);
                }
            }
        });
//        跳转到注册页面
        registerbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
            }
        });
    }

    private void showToast(String s) {
        Toast.makeText(LoginActivity.this, s, Toast.LENGTH_SHORT).show();

    }

    //    判断方法
    private User confirmUser(String usn, String psd) {
        for (User u : LitePal.findAll(User.class)) {
            if (usn.equals(u.getUsername()) && psd.equals(u.getPassword())) {
                return u;
            }
        }
        return null;
    }

    private void initView() {
        usnedit = findViewById(R.id.usnedit);
        psdedit = findViewById(R.id.psdedit);
        loginbtn = findViewById(R.id.loginbtn);
        registerbtn = findViewById(R.id.registerbtn);
    }
}
